﻿//onload
$(function () {
    if ($("input[title='Title']").length > 0) {
        //Events for NewForm and EditForm

        //Color
        $().SPServices.SPArrangeChoices({
            columnName: "Color",
            perRow: 3
        });

        $("td:contains(Color):last").next().find("label").each(function () {
            var $this = $(this),
                            strColor = $this.text();
            var renderHtml = "<div style='width: 150px;'><div style='float:left;' class='" + strColor + "'><div style='width: 10px; height:10px;border: 1px solid #BBBBBB;' class='accentBg'></div></div><div style='padding-left: 5px;float: left;'>" + strColor + "</div></div>";
            $this.prev().css("float", "left");
            $this.html(renderHtml);
        });

        //Data Source
        var $dataSource = $("td:contains(Data Source):last").next().find("input");
        $dataSource.hide();
        $dataSource.after("<select class='listCollection'></select>");
        $().SPServices({
            operation: "GetListCollection",
            async: false,
            completefunc: function (xData, Status) {
                $(xData.responseXML).SPFilterNode("List").each(function () {
                    var featureId = $(this).attr("FeatureId"),
                        serverTemplate = $(this).attr("ServerTemplate"),
                        title = $(this).attr("Title"),
                        id = $(this).attr("ID");

                    //list all Banner list definitions, depend on 2 feature RootWeb and Web
                    if ((featureId == 'fa95c38c-f0a5-4cfc-bba2-ef408ae68fbd' || featureId == '9379fcb7-a8a3-4786-8390-69b7a9d0c4db') && serverTemplate == '109') {
                        var selectedStr = "";
                        if ($dataSource.val() == id + ";#" + title) selectedStr = " selected='selected'";

                        $(".listCollection").append("<option value='" + id + "' " + selectedStr + ">" + title + "</option>");
                    }
                });
            }
        });
        $(".listCollection").on('change', function (e) {
            var $selectedOpt = $(this).find("option:selected");
            $dataSource.val($selectedOpt.val() + ";#" + $selectedOpt.text());
        });
        $(".listCollection").change();

        //Tile Content Type
        $("td:contains(Tile Content Type):last").next().find("select").on('change', function (e) {
            var $this = $(this),
                selectedVal = $this.find("option:selected").val();

            if (selectedVal == 'Banner') {
                //show BannerURL
                $("td:contains(Data Source):last").parent().show();
                $("td:contains(Item Limit):last").parent().hide();
                $("td:contains(Content):last").parent().hide();
                $("td:contains(Hyperlink):last").parent().hide();
                $("td:contains(Image):last").parent().hide();

                $("td:contains(Data Mode):last").parent().hide();
                $("td:contains(Data Speed):last").parent().show();
                $("td:contains(Data Delay):last").parent().show();
                $("td:contains(Data Direction):last").parent().show();
            }
            else if (selectedVal == 'Custom') {
                //show Content, Hyperlink, Image
                $("td:contains(Data Source):last").parent().hide();
                $("td:contains(Item Limit):last").parent().hide();
                $("td:contains(Content):last").parent().show();
                $("td:contains(Hyperlink):last").parent().show();
                $("td:contains(Image):last").parent().show();

                $("td:contains(Data Mode):last").parent().hide();
                $("td:contains(Data Speed):last").parent().hide();
                $("td:contains(Data Delay):last").parent().hide();
                $("td:contains(Data Direction):last").parent().hide();
            }
            else {
                //show Item Limit
                $("td:contains(Data Source):last").parent().hide();
                $("td:contains(Item Limit):last").parent().show();
                $("td:contains(Content):last").parent().hide();
                $("td:contains(Hyperlink):last").parent().hide();
                $("td:contains(Image):last").parent().hide();

                $("td:contains(Data Mode):last").parent().show();
                $("td:contains(Data Speed):last").parent().show();
                $("td:contains(Data Delay):last").parent().show();
                $("td:contains(Data Direction):last").parent().show();
            }
        });

        $("td:contains(Tile Content Type):last").next().find("select").change();
    }
    else {
        //Events for DispForm

        //Color
        var $colorBody = $("td:contains(Color):last").next(),
            strColor = $.trim($colorBody.text());
        var renderHtmlColor = "<div style='width: 150px;'><div style='float:left;' class='" + strColor + "'><div style='width: 10px; height:10px;border: 1px solid #BBBBBB;' class='accentBg'></div></div><div style='padding-left: 5px;float: left;'>" + strColor + "</div></div>";
        $colorBody.html(renderHtmlColor);

        //Data Source
        var $dataSourceBody = $("td:contains(Data Source):last").next(),
            strDataSource = $.trim($dataSourceBody.text()),
            arrDataSource = strDataSource.split(";#");
        if (arrDataSource.length > 1) {
            $dataSourceBody.html("<a href='" + $().SPServices.SPGetCurrentSite() + "/" + arrDataSource[1] + "' target='_blank'>" + arrDataSource[1] + "</a>");
        }

        //Tile Content Type
        var $tileContentTypeBody = $("td:contains(Tile Content Type):last").next(),
            valTileContentType = $.trim($tileContentTypeBody.text());
        if (valTileContentType == 'Banner') {
            //show BannerURL
            $("td:contains(Data Source):last").parent().show();
            $("td:contains(Item Limit):last").parent().hide();
            $("td:contains(Content):last").parent().hide();
            $("td:contains(Hyperlink):last").parent().hide();
            $("td:contains(Image):last").parent().hide();

            $("td:contains(Data Mode):last").parent().hide();
            $("td:contains(Data Speed):last").parent().show();
            $("td:contains(Data Delay):last").parent().show();
            $("td:contains(Data Direction):last").parent().show();
        }
        else if (valTileContentType == 'Custom') {
            //show Content, Hyperlink, Image
            $("td:contains(Data Source):last").parent().hide();
            $("td:contains(Item Limit):last").parent().hide();
            $("td:contains(Content):last").parent().show();
            $("td:contains(Hyperlink):last").parent().show();
            $("td:contains(Image):last").parent().show();

            $("td:contains(Data Mode):last").parent().hide();
            $("td:contains(Data Speed):last").parent().hide();
            $("td:contains(Data Delay):last").parent().hide();
            $("td:contains(Data Direction):last").parent().hide();
        }
        else {
            //show Item Limit
            $("td:contains(Data Source):last").parent().hide();
            $("td:contains(Item Limit):last").parent().show();
            $("td:contains(Content):last").parent().hide();
            $("td:contains(Hyperlink):last").parent().hide();
            $("td:contains(Image):last").parent().hide();

            $("td:contains(Data Mode):last").parent().show();
            $("td:contains(Data Speed):last").parent().show();
            $("td:contains(Data Delay):last").parent().show();
            $("td:contains(Data Direction):last").parent().show();
        }
    }
});